package com.sitech.portal.system.action;

import java.util.HashMap;
import java.util.List;

import com.sitech.ibnms.core.JGridBaseAction;
import com.sitech.portal.system.form.RoleProcessForm;
import com.sitech.portal.system.service.IRoleProcessService;

public class RoleProcessAction extends JGridBaseAction {
	
	private RoleProcessForm theForm;
	private IRoleProcessService roleProcessService;
	private List roleProcessList;
	private List processList;
	private String flag;

	public RoleProcessForm getTheForm() {
		return theForm;
	}

	public void setTheForm(RoleProcessForm theForm) {
		this.theForm = theForm;
	}

	public IRoleProcessService getRoleProcessService() {
		return roleProcessService;
	}

	public void setRoleProcessService(IRoleProcessService roleProcessService) {
		this.roleProcessService = roleProcessService;
	}

	public List getRoleProcessList() {
		return roleProcessList;
	}

	public void setRoleProcessList(List roleProcessList) {
		this.roleProcessList = roleProcessList;
	}

	public List getProcessList() {
		return processList;
	}

	public void setProcessList(List processList) {
		this.processList = processList;
	}

	public String getFlag() {
		return flag;
	}

	public void setFlag(String flag) {
		this.flag = flag;
	}

	/**
	 * 得到指定角色ID的流程列表
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String queryListForRoleProcess() {
		roleProcessList = roleProcessService.queryListForRoleProcess(theForm
				.getRoleId());
		processList = roleProcessService
				.queryProcessListForRoleProcess(theForm.getRoleId());
		// 组成hadSelect
		if (null != roleProcessList && roleProcessList.size() > 0) {
			HashMap tempMap = null;
			int iSize = roleProcessList.size();
			for (int i = 0; i < iSize; i++) {
				tempMap = (HashMap) roleProcessList.get(i);
				flag += (String) tempMap.get("PROCESS_ID") + ",";
			}
			// 去掉最后一个逗号
			flag = flag.substring(0, flag.length() - 1);
		}
		return SUCCESS;
	}

	/**
	 * 更新指定角色可用的流程
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws BaseException
	 */
	public String addListForRoleProcess() {
		flag = "true";
		try{
			String roleId = this.getRequest().getParameter("roleId");
			String hadSelectProcessIds = this.getRequest().getParameter("hadSelect");
			// 删除指定角色的所有流程
			roleProcessService.delForRoleProcess(roleId);
			// 插入值
			String[] arrHadSelectProcessId = hadSelectProcessIds.split(",");
			int iLength = arrHadSelectProcessId.length;
			HashMap<String, String> pramatersMap = new HashMap<String, String>();
			pramatersMap.put("roleId", roleId);
			for (int i = 0; i < iLength; i++) {
				pramatersMap.put("processId", arrHadSelectProcessId[i]);
				roleProcessService.addForRoleProcess(pramatersMap);
			}
		}catch(Exception e){
			flag = e.getMessage();
		}
		return SUCCESS;
	}

}
